package com.hmbe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmbe.dto.Result;
import com.hmbe.dto.StatusCode;
import com.hmbe.mapper.HmBeCategoryMapper;
import com.hmbe.entity.HmBeCategory;
import com.hmbe.service.HmBeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class HmBeCategoryServiceImpl extends ServiceImpl<HmBeCategoryMapper, HmBeCategory> implements HmBeCategoryService {
    @Autowired
    private HmBeCategoryMapper hmBeCategoryMapper;

    @Override
    public Result getCategoryList(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("Authorization");
        String userId = getUserId(token);
        //根据userId获取该登录用户的分类
        List<HmBeCategory> list = hmBeCategoryMapper.selectList(new QueryWrapper<HmBeCategory>().eq("user_id", userId));
        if (list != null && !list.isEmpty()) {
            return Result.ok("获取文章分类列表成功！", StatusCode.SUCCESS, list);

        }
        return Result.fail("获取文章分类列表失败！", StatusCode.FAILURE);
    }

    private String getUserId(String token) {
        //token字符串中取出id
        String[] split = token.split("id");
        return split[1];
    }
}
